//
//  HWScanViewController.swift
//  HWPromoteProjectAPP
//
//  Created by 王昌健 on 2022/2/11.
//  Copyright © 2022 王昌健. All rights reserved.
//

import UIKit

import AVFoundation
class HWScanViewController: LBXScanViewController {
    var navigationView:HWNavigationView!
    override func viewDidLoad() {
        super.viewDidLoad()

        initView()
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.setNavigationBarHidden(false, animated: false)
    }

    override func handleCodeResult(arrayResult: [LBXScanResult]) {
        if let result = arrayResult.first {
            let msg = result.strScanned
            print("扫描结果:" + msg!)
            self.navigationController?.popViewController(animated: true)
        }
    }

}
extension HWScanViewController {
    func initView() -> Void {
        view.backgroundColor = .white

//        navigationView = HWNavigationView()
//        navigationView.titleLabel.text = "扫描二维码"
//        view.addSubview(navigationView)
        title = "扫描二维码"
//        var style = LBXScanViewStyle()
//        style.centerUpOffset = 44
//        style.photoframeAngleStyle = LBXScanViewPhotoframeAngleStyle.Inner
//        style.photoframeLineW = 2
//        style.photoframeAngleW = 18
//        style.photoframeAngleH = 18
//        style.isNeedShowRetangle = false
//        
//        style.anmiationStyle = .LineMove
//        style.animationImage = UIImage(named: "qrcode_Scan_weixin_Line")
//        scanStyle = style

//        navigationView.snp.makeConstraints { make in
//            make.left.right.equalTo(0)
//            make.top.equalTo(0)
//        }

//        navigationView.backButtonClick = {
//            self.navigationController?.popViewController(animated: true)
//        }

//        view.bringSubviewToFront(navigationView)

    }


}
//public protocol LBXScanViewControllerDelegate: AnyObject {
//     func scanFinished(scanResult: LBXScanResult, error: String?)
//}
//
//public protocol QRRectDelegate {
//    func drawwed()
//}
//class HWScanViewController:HWBaseViewController {
//    // 返回扫码结果，也可以通过继承本控制器，改写该handleCodeResult方法即可
//    open weak var scanResultDelegate: LBXScanViewControllerDelegate?
//
//    open var delegate: QRRectDelegate?
//
//    open var scanObj: LBXScanWrapper?
//
//    open var scanStyle: LBXScanViewStyle? = LBXScanViewStyle()
//
//    open var qRScanView: LBXScanView?
//
//    // 启动区域识别功能
//    open var isOpenInterestRect = false
//
//    //连续扫码
//    open var isSupportContinuous = false;
//
//    // 识别码的类型
//    public var arrayCodeType: [AVMetadataObject.ObjectType]?
//
//    // 是否需要识别后的当前图像
//    public var isNeedCodeImage = false
//
//    // 相机启动提示文字
//    public var readyString: String! = "loading"
//
//    open override func viewDidLoad() {
//        super.viewDidLoad()
//
//        // Do any additional setup after loading the view.
//
//        // [self.view addSubview:_qRScanView];
//        view.backgroundColor = UIColor.black
//        edgesForExtendedLayout = UIRectEdge(rawValue: 0)
//    }
//
//    open func setNeedCodeImage(needCodeImg: Bool) {
//        isNeedCodeImage = needCodeImg
//    }
//
//    // 设置框内识别
//    open func setOpenInterestRect(isOpen: Bool) {
//        isOpenInterestRect = isOpen
//    }
//
//    open override func viewDidAppear(_ animated: Bool) {
//        super.viewDidAppear(animated)
//        drawScanView()
//        perform(#selector(LBXScanViewController.startScan), with: nil, afterDelay: 0.3)
//    }
//
//    @objc open func startScan() {
//        if scanObj == nil {
//            var cropRect = CGRect.zero
//            if isOpenInterestRect {
//                cropRect = LBXScanView.getScanRectWithPreView(preView: view, style: scanStyle!)
//            }
//
//            // 指定识别几种码
//            if arrayCodeType == nil {
//                arrayCodeType = [AVMetadataObject.ObjectType.qr as NSString,
//                                 AVMetadataObject.ObjectType.ean13 as NSString,
//                                 AVMetadataObject.ObjectType.code128 as NSString] as [AVMetadataObject.ObjectType]
//            }
//
//            scanObj = LBXScanWrapper(videoPreView: view,
//                                     objType: arrayCodeType!,
//                                     isCaptureImg: isNeedCodeImage,
//                                     cropRect: cropRect,
//                                     success: { [weak self] (arrayResult) -> Void in
//                                         guard let strongSelf = self else {
//                                             return
//                                         }
//                                         // 停止扫描动画
//                                         strongSelf.qRScanView?.stopScanAnimation()
//                                         strongSelf.handleCodeResult(arrayResult: arrayResult)
//            })
//
//
//        }
//
//        scanObj?.supportContinuous = isSupportContinuous;
//
//        // 结束相机等待提示
//        qRScanView?.deviceStopReadying()
//
//        // 开始扫描动画
//        qRScanView?.startScanAnimation()
//
//        // 相机运行
//        scanObj?.start()
//    }
//
//    open func drawScanView() {
//        if qRScanView == nil {
//            qRScanView = LBXScanView(frame: view.frame, vstyle: scanStyle!)
//            view.addSubview(qRScanView!)
//            delegate?.drawwed()
//        }
//        qRScanView?.deviceStartReadying(readyStr: readyString)
//    }
//
//
//    /**
//     处理扫码结果，如果是继承本控制器的，可以重写该方法,作出相应地处理，或者设置delegate作出相应处理
//     */
//    open func handleCodeResult(arrayResult: [LBXScanResult]) {
//        guard let delegate = scanResultDelegate else {
//            fatalError("you must set scanResultDelegate or override this method without super keyword")
//        }
//
//        if !isSupportContinuous {
//            navigationController?.popViewController(animated: true)
//
//        }
//
//        if let result = arrayResult.first {
//            delegate.scanFinished(scanResult: result, error: nil)
//        } else {
//            let result = LBXScanResult(str: nil, img: nil, barCodeType: nil, corner: nil)
//            delegate.scanFinished(scanResult: result, error: "no scan result")
//        }
//    }
//
//    open override func viewWillDisappear(_ animated: Bool) {
//        NSObject.cancelPreviousPerformRequests(withTarget: self)
//        qRScanView?.stopScanAnimation()
//        scanObj?.stop()
//    }
//
//    @objc open func openPhotoAlbum() {
//        LBXPermissions.authorizePhotoWith { [weak self] _ in
//            let picker = UIImagePickerController()
//            picker.sourceType = UIImagePickerController.SourceType.photoLibrary
//            picker.delegate = self
//            picker.allowsEditing = true
//            self?.present(picker, animated: true, completion: nil)
//        }
//    }
//}
//
////extension HWScanViewController {
////
////}
////MARK: - 图片选择代理方法
//extension HWScanViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
//
//    //MARK: -----相册选择图片识别二维码 （条形码没有找到系统方法）
//    public func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]) {
//        picker.dismiss(animated: true, completion: nil)
//
//        let editedImage = info[UIImagePickerController.InfoKey.editedImage] as? UIImage
//        let originalImage = info[UIImagePickerController.InfoKey.originalImage] as? UIImage
//        guard let image = editedImage ?? originalImage else {
//            showMsg(title: nil, message: NSLocalizedString("Identify failed", comment: "Identify failed"))
//            return
//        }
//        let arrayResult = LBXScanWrapper.recognizeQRImage(image: image)
//        if !arrayResult.isEmpty {
//            handleCodeResult(arrayResult: arrayResult)
//        }
//    }
//
//}
//
////MARK: - 私有方法
//private extension HWScanViewController {
//
//    func showMsg(title: String?, message: String?) {
//        let alertController = UIAlertController(title: nil, message: message, preferredStyle: .alert)
//        let alertAction = UIAlertAction(title: NSLocalizedString("OK", comment: "OK"), style: .default, handler: nil)
//        alertController.addAction(alertAction)
//        present(alertController, animated: true, completion: nil)
//    }
//
//}
